﻿
using Acorisoft.FutureGL.MigaDB.Documents;
using NewInstanceType = KinonekoSoftware.CodariaDB.InstanceType;
using OldInstanceType = Acorisoft.FutureGL.MigaDB.Interfaces.DocumentType;

namespace KinonekoSoftware.Studio.Utils.Transformers
{
    public sealed class InstanceCacheTransformer : Transformer
    {
        private const string OldCollectionName = "idx_doc";
        private const string NewCollectionName = CodariaDB.Utils.CollectionNames.Cache_InstanceDB;


        public override void Migrate(TransformContext context)
        {
            var newCollection        = context.NewRepo.GetCollection<InstanceCache>(NewCollectionName);
            var hasOldCollection     = context.OldRepo.CollectionExists(OldCollectionName);
            var oldCollection        = context.OldRepo.GetCollection<DocumentCache>(OldCollectionName);
            var newGravatarDirectory = Path.Combine(context.NewRepoDir, "Avatars");
            var oldGravatarDirectory = Path.Combine(context.OldRepoDir, "Image");
            var oldVersion           = CheckDirectory(Path.Combine(context.NewRepoDir, "OldVersion"));
            var oldVersionAvatars    = CheckDirectory(Path.Combine(oldVersion, "Images"));
            
            var    count                = 0;
            string avatar               = null;

            //
            // 如果没有这个集合则跳过
            if (!hasOldCollection)
            {
                return;
            }

            foreach (var oldCache in oldCollection.FindAll())
            {
                if (oldCache is null)
                {
                    continue;
                }

                //
                //
                if (!string.IsNullOrEmpty(oldCache.Avatar))
                {
                    avatar = ID.Get();

                    var oldGravatar = Path.Combine(oldGravatarDirectory, oldCache.Avatar);
                    var newGravatar = Path.Combine(newGravatarDirectory, $"{avatar}.png");

                    if (File.Exists(oldGravatar))
                    {
                        File.Copy(oldGravatar, newGravatar, true);
                    }
                    else
                    {
                        //
                        // 头像文件不存在，那么干脆把这个设置为null
                        avatar = null;
                    }
                }

                var oldId = oldCache.Id;
                var newId = ID.Get();

                var newCache = new InstanceCache
                {
                    Id             = newId,
                    Name           = oldCache.Name,
                    Intro          = oldCache.Intro,
                    TimeOfCreated  = oldCache.TimeOfCreated,
                    TimeOfModified = oldCache.TimeOfModified,
                    IsDeleted      = oldCache.IsDeleted,
                    IsLocked       = oldCache.IsLocked,
                    Avatar         = avatar,
                    Type           = TransformerHelper.GetInstanceType(oldCache.Type),
                };

                if (!context.DocumentMapper.TryAdd(oldId, newId))
                {
                    Xplat.Warning($"{oldId}已经存在，不需要添加。");
                }

                //
                //
                newCollection.Insert(newCache);

                //
                //
                avatar = null;
                count++;
                
                Xplat.Debug($"设定{oldId}已经迁移成功，新的id为:{newId}");
            }
            
            //
            //
            if(Directory.Exists(oldGravatarDirectory)) Directory.Move(oldGravatarDirectory, oldVersionAvatars);
            
            //
            //
            Xplat.Info($"InstanceCache已经迁移完毕，成功迁移{count}个！");
        }
    }
}